package com.lqm.service.home.impl;

import com.lqm.dao.ProjectDao;
import com.lqm.entity.home.HomeUser;
import com.lqm.entity.home.Project;
import com.lqm.entity.home.ProjectSupport;
import com.lqm.entity.home.ProjectType;
import com.lqm.service.home.ProjectService;
import com.lqm.service.home.ProjectSupportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @program: CrowdFunding
 * @description
 * @author: lqm
 * @create: 2018-12-10 22:55
 */
@Service
//@Transactional
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    ProjectDao projectDao;


    @Override
    public Project getProjectById(Integer id) {
        return projectDao.findByProjectId(id);
    }

    @Override
    public List<Project> getAllProjectLimit(int page, int limit,Project project) {
/*        if (project==null){
            project=new Project();
            ProjectType projectType = new ProjectType();
            projectType.setTypeId(0);
            project.setProjectType(projectType);
        }*/

        Sort sort = new Sort(Sort.Direction.DESC, "projectId");
        Pageable pageable = PageRequest.of(page - 1, limit, sort);
        List<Project> list;
        if (project.getProjectType().getTypeId()==0||project.getProjectType().getTypeId()==null||project.getProjectType()==null){
             list= projectDao.findAllByStatusAndNameLike(1, "%" + project.getName() + "%", pageable);
        }else {
            list=projectDao.findAllByStatusAndProjectTypeAndNameLike(1,project.getProjectType(),"%" + project.getName() + "%", pageable);
        }

        return list;
    }

    @Override
    public Integer getAllProjectLimitCount(Project project) {
        Integer count;
        if (project.getProjectType().getTypeId()==0||project.getProjectType().getTypeId()==null||project.getProjectType()==null){
            count= projectDao.countAllByStatusAndNameLike(1, "%" + project.getName() + "%");
        }else {
            count=projectDao.countAllByStatusAndProjectTypeAndNameLike(1,project.getProjectType(),"%" + project.getName() + "%");
        }

        return count;
    }

    @Override
    public List<Project> getAllProjectLimitAdmin(int page, int limit, Project project) {
        Sort sort = new Sort(Sort.Direction.DESC, "projectId");
        Pageable pageable = PageRequest.of(page - 1, limit, sort);
        List<Project> list;
        if (project.getProjectType().getTypeId()==0||project.getProjectType().getTypeId()==null||project.getProjectType()==null){
            list= projectDao.findAllByNameLike("%" + project.getName() + "%", pageable);
        }else {
            list=projectDao.findAllByProjectTypeAndNameLike(project.getProjectType(),"%" + project.getName() + "%", pageable);
        }

        return list;
    }

    @Override
    public Integer getAllProjectLimitCountAdmin(Project project) {
        Integer count;
        if (project.getProjectType().getTypeId()==0||project.getProjectType().getTypeId()==null||project.getProjectType()==null){
            count= projectDao.countAllByNameLike( "%" + project.getName() + "%");
        }else {
            count=projectDao.countAllByProjectTypeAndNameLike(project.getProjectType(),"%" + project.getName() + "%");
        }

        return count;
    }

    @Override
    public Integer setProjectStatus(Project project, int status) {
        return projectDao.setProjectStatus(project,status);
    }

    @Override
    public List<Project> getAllSuccessProjectLimit(int page, int limit) {
        Sort sort = new Sort(Sort.Direction.DESC, "projectId");
        Pageable pageable = PageRequest.of(page - 1, limit, sort);
        return projectDao.findAllByStatusAndProjectStatus(1,2,pageable);
    }

    @Override
    public Project saveProject(Project project) {
        return projectDao.save(project);
    }

    @Override
    public List<Project> getUserProjectByPage(int page, int limit, HomeUser user) {
        Sort sort = new Sort(Sort.Direction.DESC, "projectId");
        Pageable pageable = PageRequest.of(page - 1, limit, sort);
        return projectDao.findByStatusAndHomeUser(1,user,pageable);
    }

    @Override
    public Integer getUserProjectCount(HomeUser homeUser) {
        Integer count = projectDao.getUserProjectCount(homeUser);
        return count==null?0:count;
    }

    @Override
    @Transactional
    public boolean toPass(Project project) {
        Integer integer = projectDao.setProjectStatus(project, 1);
        if (integer!=null&&integer!=0){
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public boolean notToPass(Project project) {
        Integer integer = projectDao.setProjectStatus(project, -1);
        if (integer!=null&&integer!=0){
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public boolean delProjectByHomeUserAndProject(Project project) {



        Integer integer = projectDao.setProjectStatus(project,-2);
        if (integer==null){
            return false;
        }
        if (integer>0){
            return true;
        }
        return false;
    }
}
